package client;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.nio.charset.Charset;

public class MyClientEncoder extends ChannelInboundHandlerAdapter {

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        for (int i = 0; i < 1000; i++) {
            ByteBuf buffer = getByteBuf(ctx);
            ctx.channel().writeAndFlush(buffer);
        }
    }

    private ByteBuf getByteBuf(ChannelHandlerContext ctx) {
        byte[] bytes = "一二三四五六七八九十".getBytes(Charset.forName("utf-8"));
        ByteBuf buffer = ctx.alloc().buffer();

      //  buffer.writeBytes("\r\n".getBytes(Charset.forName("utf-8")));//换行
     //   buffer.writeBytes(new byte[10]);//模拟补码
       //   buffer.writeBytes("$".getBytes(Charset.forName("utf-8")));//特殊字符换行
        byte[] bytes1 = new byte[7];//模拟7个偏移量字节,一般用于魔数,版本号,序列化算法,指令等
        buffer.writeBytes(bytes1);
        buffer.writeInt(bytes.length);//数据长度
        buffer.writeBytes(bytes);//写数据本身
        return buffer;
    }

}
